package com.zb.controller;

import com.zb.entity.TbOrderItemModel;
import com.zb.entity.TbOrderModel;
import com.zb.service.CartService;
import com.zb.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/add")
    public Boolean addCart(@RequestParam("num") Integer num, @RequestParam("skuId") String skuId) {
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.addItem(skuId, num, username);
    }

    @GetMapping("/list")
    public List<TbOrderItemModel> list() {
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.list(username);
    }

    @GetMapping("/select")
    public List<TbOrderItemModel> select(@RequestParam("skuIds") String [] skuIds) {
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.select(username,skuIds);
    }


    @GetMapping("/remove")
    public Boolean remove(@RequestParam("skuId") String skuId) {
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.remove(username, skuId);
    }

}
